package com.teradata.dashboard.ui.fragment;

import java.lang.reflect.Field;

import android.app.ActionBar;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.teradata.dashboard.R;

public class OrderFragment extends Fragment {
	
	 @Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,  
	            Bundle savedInstanceState) {  
		 	View orderLayout = inflater.inflate(R.layout.order, container, false);
			System.out.println("kpi input");
			getOverflowMenu();
			initHead();
			
			showKpiList("2222222222");
			return orderLayout; 
	    }
	 
		private void initHead() {
			final ActionBar actionBar = getActivity().getActionBar(); 
			actionBar.setDisplayShowTitleEnabled(true);
			actionBar.setDisplayShowCustomEnabled(true);
			View actionBarButtons = LayoutInflater.from(getActivity()).inflate(R.layout.head,
                    new LinearLayout(getActivity()), false);
			((TextView)actionBarButtons.findViewById(R.id.tv_head)).setText(R.string.order);
			 Typeface iconfont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/fontawesome-webfont.ttf");
			 TextView leftBtn=(TextView)actionBarButtons.findViewById(R.id.btn_leftTop);
			 leftBtn.setText(R.string.returnTo_i);
			 leftBtn.setTypeface(iconfont);
			actionBarButtons.setOnClickListener(new OnClickListener() {//监听事件  
	            @Override
				public void onClick(View arg0) {  
	                switch (arg0.getId()) {  
	                case R.id.btn_leftTop:  
	                    break;  
	                default:  
	                    break;  
	                }  
	            }  
	        });  
			this.getActivity().getActionBar().setCustomView(actionBarButtons);
		}

		@Override
		public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
			super.onCreateOptionsMenu(menu, inflater);
			menu.clear();
			//menu.add(R.string.order);
		}
	 private void getOverflowMenu() {

			try {
				ViewConfiguration config = ViewConfiguration
						.get(this.getActivity());
				Field menuKeyField = ViewConfiguration.class
						.getDeclaredField("sHasPermanentMenuKey");
				if (menuKeyField != null) {
					menuKeyField.setAccessible(true);
					menuKeyField.setBoolean(config, false);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	 private void showKpiList(String categoryId) {
			KpiListFragment fragment = new KpiListFragment();
			Bundle args = new Bundle();
			args.putString(KpiFragment.SELECTED_CATEGORY_ID, categoryId);
			fragment.setArguments(args);
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			ft.replace(R.id.kpiOrderListContainer, fragment);
			ft.commit();
		}
}
